home *** CD-ROM | disk | FTP | other *** search
-
- /* File StdPrefs.c
- Standard preferences file handling routines, implementation.
- System 6 or better requires HFS
- Copyright (c) 1996, 1997 by John Montbriand. All Rights Reserved.
- Permission hereby granted for public use.
- Distribute freely in areas where the laws of copyright apply.
- USE AT YOUR OWN RISK.
- DO NOT DISTRIBUTE MODIFIED COPIES.
- Comments/questions/postcards* to the author at the address:
- John Montbriand
- P.O. Box. 1133
- Saskatoon Saskatchewan Canada
- S7K 3N2
- or by email at:
- tinyjohn@sk.sympatico.ca
- *if you mail a postcard, then I will provide you with technical support
- regarding questions you may have about this file.
- see also: <http://www3.sk.sympatico.ca/tinyjohn>
- */
-
- #include "StdPrefs.h"
- #include <Folders.h>
- #include <OSUtils.h>
- #include <Resources.h>
- #include <Gestalt.h>
- #include <String.h>
- #include <PLStringFuncs.h>
- #include <Errors.h>
- #include <TextUtils.h>
-
-
- OSErr FindPreferencesFolder(short *pvol, long *pdir) {
- OSErr err;
- long response = 0;
- *pvol = 0;
- *pdir = 0;
- if (Gestalt(gestaltFindFolderAttr, &response) != noErr) response = 0;
- if ((response & (1<<gestaltFindFolderPresent)) != 0) {
- err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, pvol, pdir);
- } else {
- long procID;
- SysEnvRec the_env;
- SysEnvirons(curSysEnvVers, &the_env);
- err = GetWDInfo(the_env.sysVRefNum, pvol, pdir, &procID);
- }
- return err;
- }
-
-
- OSErr PrefsDirSearch(OSType creator, OSType type, short *fvol, long *fdir, StringPtr fname) {
- short vol;
- long dir;
- CInfoPBRec cat;
- FInfo *fin;
- Str255 name;
- OSErr err;
-
- /* find the preferences folder */
- err = FindPreferencesFolder(&vol, &dir);
- if (err != noErr) return err;
-
- /* look for the file */
- memset(&cat, 0, sizeof(cat));
- cat.hFileInfo.ioNamePtr = name;
- cat.hFileInfo.ioVRefNum = vol;
- cat.hFileInfo.ioDirID = dir;
- cat.hFileInfo.ioFDirIndex = 1;
- fin = &cat.hFileInfo.ioFlFndrInfo;
- while (PBGetCatInfoSync(&cat) == noErr) {
- if ((cat.hFileInfo.ioFlAttrib & 16) == 0
- && fin->fdType == type
- && fin->fdCreator == creator) {
- PLstrcpy(fname, name);
- *fvol = vol;
- *fdir = dir;
- return noErr;
- }
- cat.hFileInfo.ioDirID = dir;
- cat.hFileInfo.ioFDirIndex++;
- }
- return fnfErr;
- }
-
- OSErr FSpPrefsDirSearch(OSType creator, OSType type, FSSpec *spec) {
- return PrefsDirSearch(creator, type, &spec->vRefNum, &spec->parID, spec->name);
- }
-
- OSErr PrefsCreate(OSType creator, OSType type, StringPtr defaultname, short *fvol, long *fdir, StringPtr fname) {
- OSErr err;
- long num;
- FInfo fndrInfo;
- Str255 s;
- num = 1;
- if ((err = FindPreferencesFolder(fvol, fdir)) != noErr) goto bail;
- PLstrcpy(fname, defaultname);
- while (1) {
- err = HGetFInfo(*fvol, *fdir, fname, &fndrInfo);
- if (err == fnfErr) break; else if (err != noErr) goto bail;
- PLstrcpy(fname, defaultname);
- PLstrcat(fname, "\p "); /* followed by a space */
- NumToString(num++, s);
- PLstrcat(fname, s); /* and a number */
- }
- err = HCreate(*fvol, *fdir, fname, creator, type);
- if (err != noErr) goto bail;
- HCreateResFile(*fvol, *fdir, fname);
- if ((err = ResError()) != noErr) goto bail;
- return noErr;
- bail:
- return err;
- }
-
- OSErr FSpPrefsCreate(OSType creator, OSType type, StringPtr defaultname, FSSpec *spec) {
- return PrefsCreate(creator, type, defaultname, &spec->vRefNum, &spec->parID, spec->name);
- }
-